package com.sdjictec.haier.utils;

import com.sdjictec.haier.Kafka2Oracle;
import com.sdjictec.haier.entity.RetailDataMQ;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @author 吴敬超
 * @date 2018/8/13
 * Description:
 */
public class ILogic {


    private static Logger logger = LoggerFactory.getLogger(Kafka2Oracle.class);

    public RetailDataMQ mergeDataInfo(RetailDataMQ retailDataMQ) {

        DBUtills dbUtills = new DBUtills();


        String shUniCode = retailDataMQ.getShUniCode();
        String productCode = retailDataMQ.getProductCode();

        logger.debug("shUniCode:" + shUniCode);
        logger.debug("productCode:" + productCode);

        //取门店信息(mq的shUniCode 查找origin_id, mdm_shcode不为空且 active_flag <='1',取出mdm_shcode)

        String sql = " select t.ORIGIN_ID ,t.MDM_SHCODE from BVS.ODS_ESTORE_CUST_ENTITY t " +
                " where entity_type = 0 and active_flag <='1' and   " +
                " t.ORIGIN_ID='" + shUniCode + "'";
//        List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);
        List<Map<String, Object>> maps = dbUtills.jdbcsql(sql, "tb");
        String MDM_SHCODE = "";
        if (maps != null && maps.size() >= 1) {
            MDM_SHCODE = (String) maps.get(0).get("MDM_SHCODE");
            logger.debug("MDM_SHCODE:" + MDM_SHCODE);


        }

//        String storeId = stringRedisTemplate.opsForValue().get(shUniCode);
        retailDataMQ.setStoreUniCode(MDM_SHCODE);


        String getlmsales_organizationSQL =
                " select b.lmsales_organization as org,b.LMSALES_ORGANIZATION_NAME as orgname, " +
                        " a.tmcsh_mktid as tmcsh_mktid ," +
                        " a.tmcsh_mainchannel as tmcsh_mainchannel " +
                        " from t_mdm_cus_shop a left join  lfmkt b on  b.lmmktid= a.tmcsh_mktid"
                        + " where a.tmcsh_code='" + MDM_SHCODE + "'";

//        List<Map<String, Object>> orgs = secondJdbcTemplate.queryForList(getlmsales_organizationSQL);
        List<Map<String, Object>> orgs = dbUtills.jdbcsql(getlmsales_organizationSQL, "stb");
        String org = "";
        String orgName = "";
        String tmcsh_mktid = "";
        String tmcsh_mainchannel = "";
        if ((orgs == null || orgs.size() < 1) && retailDataMQ.getSpCode() != null) {
            getlmsales_organizationSQL =
                    " select b.lmsales_organization as org,b.LMSALES_ORGANIZATION_NAME as orgname, " +
                            " a.tmcsh_mktid as tmcsh_mktid ," +
                            " a.tmcsh_mainchannel as tmcsh_mainchannel " +
                            " from t_mdm_cus_shop a left join  lfmkt b on  b.lmmktid= a.tmcsh_mktid"
                            + " where a.tmcsh_code='" + retailDataMQ.getSpCode() + "'";

//            orgs = secondJdbcTemplate.queryForList(getlmsales_organizationSQL);

            orgs = dbUtills.jdbcsql(getlmsales_organizationSQL, "stb");
        }

        if (orgs != null && orgs.size() >= 1) {
//            org = (String) orgs.get(0).get("org");
//            orgName = (String) orgs.get(0).get("orgName");
//            tmcsh_mktid = (String) orgs.get(0).get("tmcsh_mktid");
//            tmcsh_mainchannel = (String) orgs.get(0).get("tmcsh_mainchannel");

            org = (String) orgs.get(0).get("ORG");
            orgName = (String) orgs.get(0).get("ORGNAME");
            tmcsh_mktid = (String) orgs.get(0).get("TMCSH_MKTID");
            tmcsh_mainchannel = (String) orgs.get(0).get("TMCSH_MAINCHANNEL");

            logger.debug("org:" + org);
            logger.debug("orgName:" + orgName);
            logger.debug("tmcsh_mktid:" + tmcsh_mktid);
            logger.debug("tmcsh_mainchannel:" + tmcsh_mainchannel);

            retailDataMQ.setOrgCode(org);
            retailDataMQ.setOrgName(orgName);
            retailDataMQ.setTmcshMktid(tmcsh_mktid);
            retailDataMQ.setTmcshMainchannel(tmcsh_mainchannel);

            String getPriceSQL = "select  t.ib_price as ib_price " +
                    "from BVS.ODS_BCC_PRICE_AREA t "
                    + " where t.gj_code='" + org + "' and t.code='" + productCode + "'";

//            List<Map<String, Object>> prices = jdbcTemplate.queryForList(getPriceSQL);
            List<Map<String, Object>> prices = dbUtills.jdbcsql(getPriceSQL, "tb");
            Double price = 0D;
            if (prices != null && prices.size() >= 1) {
                BigDecimal bigDecimal = (BigDecimal) prices.get(0).get("IB_PRICE");
                price = bigDecimal.doubleValue();

                logger.debug("price:" + price);
                retailDataMQ.setIbPrice(price);
            }
        }

        if (retailDataMQ.getIbPrice() == null) {
//            System.out.println(retailDataMQ);

            String getPriceSQL = "select  max(t.ib_price) as ib_price " +
                    "from BVS.ODS_BCC_PRICE_AREA t "
                    + " where  t.code='" + productCode + "'";

//            List<Map<String, Object>> prices = jdbcTemplate.queryForList(getPriceSQL);
            List<Map<String, Object>> prices = dbUtills.jdbcsql(getPriceSQL, "tb");
            Double price = 0D;
            if (prices != null && prices.size() >= 1) {
                BigDecimal bigDecimal = (BigDecimal) prices.get(0).get("IB_PRICE");
                if (bigDecimal != null) {
                    price = bigDecimal.doubleValue();
                    logger.debug("price:" + price);
                    retailDataMQ.setIbPrice(price);
//                    iRetailDataMQDao.save(retailDataMQ);
                }

            }
        }

        //取产品大类
        String getDEPARTMENTSQL = "select DEPARTMENT from ETL_MDM_GOODSBASE where MATERIAL_CODE='" + productCode + "'";
//        List<Map<String, Object>> DEPARTMENTs = secondJdbcTemplate.queryForList(getDEPARTMENTSQL);

        List<Map<String, Object>> DEPARTMENTs = dbUtills.jdbcsql(getDEPARTMENTSQL, "stb");
        String DEPARTMENT = "";
        if (DEPARTMENTs != null && DEPARTMENTs.size() >= 1) {
            DEPARTMENT = (String) DEPARTMENTs.get(0).get("DEPARTMENT");

            logger.debug("DEPARTMENT:" + DEPARTMENT);
            retailDataMQ.setDepartment(DEPARTMENT);
        }

        //


        return retailDataMQ;


    }

}
